/**
 * @file comfunct.c
 * @author Yao Zongyou <yaozy2010@gmail.com>
 * @date 2011-03-21
 */

#define WIN32_LEAN_AND_MEAN
#define BUILD_COMFUNCT_DLL

#define _WIN32_WINNT 0x0500 /* For the VK_* values in the hook procedure */

#include <windows.h>
#include <wininet.h>
#include <commctrl.h>
#include <shlwapi.h>

#include "comfunct.h"

typedef const TCHAR * (__cdecl * TRANSLATEPROC)(unsigned int);
TRANSLATEPROC tpGetText = NULL;
HINSTANCE hLanguageDll = NULL;

BOOL CALLBACK DllMain(HINSTANCE hInst, DWORD dwReason, LPVOID lpNull)
{
	switch (dwReason) {
		case DLL_PROCESS_DETACH:
			FreeLibrary(hLanguageDll);
			break;
	}

	return TRUE;
}

EXPORT_COMFUNCT void __cdecl SetStatusA(HWND hwnd, UINT iStatusIdent, int iBarPart, 
			LPCSTR message)
{
}

EXPORT_COMFUNCT HINTERNET __cdecl ConnectHandleA(LPCSTR szUserAgent)
{
	return NULL;
}

EXPORT_COMFUNCT HINTERNET __cdecl ServerConnectA(HINTERNET hInitHandle, LPCSTR szHostName)
{
	return NULL;
}

EXPORT_COMFUNCT void __cdecl SetStatusW(HWND hwnd, UINT iStatusIdent, int iBarPart, LPCWSTR message)
{
	return;
}

EXPORT_COMFUNCT HINTERNET __cdecl ConnectHandleW(LPCWSTR szUserAgent)
{
	return NULL;
}

EXPORT_COMFUNCT HINTERNET __cdecl ServerConnectW(HINTERNET hInitHandle, LPCWSTR szHostName)
{
	return NULL;
}

EXPORT_COMFUNCT char * __cdecl ASCIIConversion(LPCWSTR wszWideString)
{
	return NULL;
}

EXPORT_COMFUNCT WCHAR * __cdecl UnicodeConversion(LPCSTR szNarrowString)
{
	return NULL;
}

BOOL CALLBACK RealChangeTextFont(HWND hwnd, LPARAM hfFont)
{
	SendMessage(hwnd, WM_SETFONT, (WPARAM)hfFont, MAKELPARAM(TRUE, 0));
	return TRUE;
}

EXPORT_COMFUNCT void __cdecl ChangeTextFont(HWND hwnd, HFONT hfFont)
{
	EnumChildWindows(hwnd, RealChangeTextFont, (LPARAM)hfFont);
}

EXPORT_COMFUNCT void __cdecl FreePointer(void *lpMemToFree)
{

}

EXPORT_COMFUNCT const char * __cdecl GetLocalTextA(unsigned int iIndex)
{
	return (const char *)tpGetText(iIndex);
}

EXPORT_COMFUNCT const WCHAR * __cdecl GetLocalTextW(unsigned int iIndex)
{
	return (const WCHAR *)tpGetText(iIndex);
}

EXPORT_COMFUNCT void __cdecl SetupLanguage(void)
{
	WIN32_FIND_DATAA wfd;
	HANDLE hFile = NULL;
	char szFile[MAX_PATH], szSearchParam[MAX_PATH];
	szFile[0] = 0;
	szSearchParam[0] = 0;

	GetModuleFileNameA(GetModuleHandleA(NULL), szSearchParam, MAX_PATH);
	PathRemoveFileSpecA(szSearchParam);
	lstrcatA(szSearchParam, "\\");
	lstrcpyA(szFile, szSearchParam);
	lstrcatA(szSearchParam, "lang_*.*");
	hFile = FindFirstFileA(szSearchParam, &wfd);
	do {
		if (StrStrIA(wfd.cFileName, ".dll") != NULL) {
			lstrcatA(szFile, wfd.cFileName);
			break;
		}
	} while (FindNextFileA(hFile, &wfd) != 0);
	FindClose(hFile);
	hLanguageDll = LoadLibraryA(szFile);
	if (hLanguageDll == NULL) {
		MessageBoxA(NULL, "No Language DLL found, AudioPlayer will now close", "AudioPlayer Error", MB_OK | MB_ICONERROR);
		ExitProcess(-1);
	}
	tpGetText = (TRANSLATEPROC)GetProcAddress(hLanguageDll, "GetIndexedLocalString");
	return;
}

EXPORT_COMFUNCT char * __cdecl GetCurrentLanguage(void)
{
	return NULL;
}
